home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 2 of 3.iso / chapter9 / crupdown.c < prev    next >
C/C++ Source or Header  |  1996-03-06  |  13KB  |  355 lines

  1.  
  2. #include <windows.h>  
  3. #include <commctrl.h>
  4. #include "crupdown.h"  
  5.  
  6.  
  7. #if defined (WIN32)
  8.     #define IS_WIN32 TRUE
  9. #else
  10.     #define IS_WIN32 FALSE
  11. #endif
  12.  
  13. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  14. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  15. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  16.  
  17. HINSTANCE hInst;   // current instance
  18.  
  19. LPCTSTR lpszAppName = "MyApp";
  20. LPCTSTR lpszTitle   = "CreateUpDownControl()"; 
  21.  
  22.  
  23. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  24.  
  25.  
  26. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  27.                       LPTSTR lpCmdLine, int nCmdShow)
  28. {
  29.    MSG      msg;
  30.    HWND     hWnd; 
  31.    WNDCLASS wc;
  32.  
  33.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  34.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  35.    wc.cbClsExtra    = 0;                      
  36.    wc.cbWndExtra    = 0;                      
  37.    wc.hInstance     = hInstance;              
  38.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  39.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  40.    wc.hbrBackground = (HBRUSH)(COLOR_3DFACE+1);
  41.    wc.lpszMenuName  = lpszAppName;              
  42.    wc.lpszClassName = lpszAppName;              
  43.  
  44.    if ( IS_WIN95 )
  45.    {
  46.       if ( !RegisterWin95( &wc ) )
  47.          return( FALSE );
  48.    }
  49.    else if ( !RegisterClass( &wc ) )
  50.       return( FALSE );
  51.  
  52.    hInst = hInstance; 
  53.  
  54.    hWnd = CreateWindow( lpszAppName, 
  55.                         lpszTitle,    
  56.                         WS_OVERLAPPEDWINDOW, 
  57.                         CW_USEDEFAULT, 0, 
  58.                         CW_USEDEFAULT, 0,  
  59.                         NULL,              
  60.                         NULL,              
  61.                         hInstance,         
  62.                         NULL               
  63.                       );
  64.  
  65.    if ( !hWnd ) 
  66.       return( FALSE );
  67.  
  68.    ShowWindow( hWnd, nCmdShow ); 
  69.    UpdateWindow( hWnd );         
  70.  
  71.    while( GetMessage( &msg, NULL, 0, 0) )   
  72.    {
  73.       TranslateMessage( &msg ); 
  74.       DispatchMessage( &msg );  
  75.    }
  76.  
  77.    return( msg.wParam ); 
  78. }
  79.  
  80.  
  81. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  82. {
  83.    WNDCLASSEX wcex;
  84.  
  85.    wcex.style         = lpwc->style;
  86.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  87.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  88.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  89.    wcex.hInstance     = lpwc->hInstance;
  90.    wcex.hIcon         = lpwc->hIcon;
  91.    wcex.hCursor       = lpwc->hCursor;
  92.    wcex.hbrBackground = lpwc->hbrBackground;
  93.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  94.    wcex.lpszClassName = lpwc->lpszClassName;
  95.  
  96.    // Added elements for Windows 95.
  97.    //...............................
  98.    wcex.cbSize = sizeof(WNDCLASSEX);
  99.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  100.                             IMAGE_ICON, 16, 16,
  101.                             LR_DEFAULTCOLOR );
  102.             
  103.    return RegisterClassEx( &wcex );
  104. }
  105.  
  106.  
  107. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  108. {
  109. static HWND hEdit   = NULL;
  110. static HWND hUpDown = NULL;
  111.  
  112.    switch( uMsg )
  113.    {
  114.       case WM_CREATE :
  115.               // Create the edit control.
  116.               //.........................
  117.               hEdit = CreateWindowEx( WS_EX_CLIENTEDGE, "EDIT", "",
  118.                                       WS_CHILD | WS_BORDER | WS_VISIBLE,
  119.                                       40, 40, 70, 25, hWnd, 
  120.                                       (HMENU)IDC_EDIT, hInst, NULL );
  121.  
  122.               // Create the up-down control.
  123.               //............................
  124.               hUpDown = CreateUpDownControl( WS_CHILD | WS_BORDER | 
  125.                                              WS_VISIBLE |
  126.                                              UDS_ALIGNRIGHT,
  127.                                              0, 0, 18, 25, hWnd, IDC_UPDOWN,
  128.                                              hInst, NULL,
  129.                                              100, 1, 1 );
  130.  
  131.               // The buddy window could have been specified with the 
  132.               // CreateUpDownControl() function. However for this
  133.               // example, we will use the UDM_SETBUDDY message.
  134.               //....................................................
  135.               SendMessage( hUpDown, UDM_SETBUDDY, (WPARAM)hEdit, 0 );
  136.  
  137.               // Initialize the edit control to the beginning value.
  138.               //....................................................
  139.               SendMessage( hEdit, WM_SETTEXT, 0, (LPARAM)"1" );
  140.               break;
  141.  
  142.       case WM_VSCROLL :
  143.               if ( (HWND)lParam == hUpDown )
  144.               {
  145.                  HWND  hBuddyWnd;
  146.                  TCHAR szTmp[10];
  147.                  
  148.                  // Retrieve the buddy window. This is useful if we
  149.                  // are not sure which buddy window the scroll is for.
  150.                  //...................................................
  151.                  hBuddyWnd = (HWND)SendMessage( hUpDown, UDM_GETBUDDY, 0, 0 );
  152.  
  153.                  // Find out the base of the up/down control and 
  154.                  // format the string for the buddy window.
  155.                  //.............................................
  156.                  if ( SendMessage( hUpDown, UDM_GETBASE, 0, 0 ) == 10 )
  157.                     wsprintf( szTmp, "%d", HIWORD( wParam ) );
  158.                  else
  159.                     wsprintf( szTmp, "%X", HIWORD( wParam ) );
  160.  
  161.                  // Set the new value into the edit control 
  162.                  // and make it the selected text.
  163.                  //........................................
  164.                  SendMessage( hBuddyWnd, WM_SETTEXT, 0, (LPARAM)szTmp );
  165.                  SendMessage( hBuddyWnd, EM_SETSEL, 0, -1 );
  166.               }
  167.               break;
  168.  
  169.       case WM_NOTIFY :
  170.               {
  171.                  NM_UPDOWN* ud = (NM_UPDOWN*)lParam;
  172.  
  173.                  if ( ud->hdr.code == UDN_DELTAPOS )
  174.                  {
  175.                     // Disallow the value of 10 by modifying
  176.                     // the iDelta value to skip over it.
  177.                     //......................................
  178.                     if ( (ud->iPos + ud->iDelta) == 10 )
  179.                     {
  180.                        if ( ud->iDelta > 0 )
  181.                           ud->iDelta++;
  182.                        else
  183.                           ud->iDelta--;
  184.                     }
  185.                  }
  186.               }
  187.               break;
  188.  
  189.       case WM_COMMAND :
  190.               switch( LOWORD( wParam ) )
  191.               {
  192.                  case IDM_OPTIONS :
  193.                         DialogBox( hInst, "OptionsDlg", hWnd, (DLGPROC)Options );
  194.                         break;
  195.  
  196.                  case IDM_ABOUT :
  197.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  198.                         break;
  199.  
  200.                  case IDM_EXIT :
  201.                         DestroyWindow( hWnd );
  202.                         break;
  203.               }
  204.               break;
  205.       
  206.       case WM_DESTROY :
  207.               PostQuitMessage(0);
  208.               break;
  209.  
  210.       default :
  211.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  212.    }
  213.  
  214.    return( 0L );
  215. }
  216.  
  217.  
  218. LRESULT CALLBACK Options( HWND hDlg,           
  219.                           UINT message,        
  220.                           WPARAM wParam,       
  221.                           LPARAM lParam)
  222. {
  223.    switch (message) 
  224.    {
  225.        case WM_INITDIALOG: 
  226.                {
  227.                   HWND    hUpDown;
  228.                   UDACCEL accel[2];
  229.                   DWORD   dwRange;
  230.                   DWORD   dwPos;
  231.                   DWORD   dwBase;
  232.                   int     nNumAccel;
  233.  
  234.                   // Retrieve the up/down control.
  235.                   //..............................
  236.                   hUpDown = GetDlgItem( GetParent( hDlg ), IDC_UPDOWN );
  237.  
  238.                   // Retrieve the acceleration information.
  239.                   //.......................................
  240.                   nNumAccel = SendMessage( hUpDown, UDM_GETACCEL, 
  241.                                            2, (LPARAM)accel );
  242.                   
  243.                   // Retrieve the range.
  244.                   //....................
  245.                   dwRange = SendMessage( hUpDown, UDM_GETRANGE, 0, 0 );
  246.  
  247.                   // Retrieve the current position.
  248.                   //...............................
  249.                   dwPos = SendMessage( hUpDown, UDM_GETPOS, 0, 0 );
  250.  
  251.                   // Retrieve the current base.
  252.                   //...........................
  253.                   dwBase = SendMessage( hUpDown, UDM_GETBASE, 0, 0 );
  254.  
  255.                   // Initialize the dialog controls.
  256.                   //................................
  257.                   SetDlgItemInt( hDlg, IDC_RANGE1, HIWORD( dwRange ), FALSE );  
  258.                   SetDlgItemInt( hDlg, IDC_RANGE2, LOWORD( dwRange ), FALSE );  
  259.                   SetDlgItemInt( hDlg, IDC_POS,    LOWORD( dwPos ), FALSE );  
  260.  
  261.                   SetDlgItemInt( hDlg, IDC_SEC1, accel[0].nSec, FALSE );
  262.                   SetDlgItemInt( hDlg, IDC_INC1, accel[0].nInc, FALSE );
  263.                   if ( nNumAccel > 1 )
  264.                   {
  265.                      SetDlgItemInt( hDlg, IDC_SEC2, accel[1].nSec, FALSE );
  266.                      SetDlgItemInt( hDlg, IDC_INC2, accel[1].nInc, FALSE );
  267.                   }
  268.  
  269.                   CheckDlgButton( hDlg, IDC_HEX, 
  270.                                   dwBase == 16 ? BST_CHECKED : BST_UNCHECKED );
  271.                }
  272.                return (TRUE);
  273.  
  274.        case WM_COMMAND:                              
  275.                switch( LOWORD( wParam ) )
  276.                {
  277.                   case IDOK :
  278.                          {
  279.                             HWND    hUpDown;
  280.                             UDACCEL accel[2];
  281.                             DWORD   dwRange;
  282.                             DWORD   dwPos;
  283.                             DWORD   dwBase;
  284.                             int     nNumAccel;
  285.  
  286.                             // Retrieve the up/down control.
  287.                             //..............................
  288.                             hUpDown = GetDlgItem( GetParent( hDlg ), IDC_UPDOWN );
  289.  
  290.                             // Retrieve the information from the dialog.
  291.                             //..........................................
  292.                             dwRange = MAKELONG( 
  293.                                          (WORD)GetDlgItemInt( hDlg, IDC_RANGE2, NULL, FALSE ),
  294.                                          (WORD)GetDlgItemInt( hDlg, IDC_RANGE1, NULL, FALSE ) );  
  295.  
  296.                             dwPos = GetDlgItemInt( hDlg, IDC_POS, NULL, FALSE );  
  297.  
  298.                             accel[0].nSec = GetDlgItemInt( hDlg, IDC_SEC1, NULL, FALSE );
  299.                             accel[0].nInc = GetDlgItemInt( hDlg, IDC_INC1, NULL, FALSE );
  300.                             accel[1].nSec = GetDlgItemInt( hDlg, IDC_SEC2, NULL, FALSE );
  301.                             accel[1].nInc = GetDlgItemInt( hDlg, IDC_INC2, NULL, FALSE );
  302.  
  303.                             nNumAccel = 1;
  304.                             if ( accel[1].nInc ) nNumAccel++;
  305.  
  306.                             if ( IsDlgButtonChecked( hDlg, IDC_HEX ) )
  307.                                dwBase = 16;
  308.                             else
  309.                                dwBase = 10;
  310.                             
  311.                             // Set the attributes for the up/down control.
  312.                             //............................................
  313.                             SendMessage( hUpDown, UDM_SETACCEL, nNumAccel, (LPARAM)accel );
  314.                             SendMessage( hUpDown, UDM_SETBASE, dwBase, 0 );
  315.                             SendMessage( hUpDown, UDM_SETPOS, 0, MAKELONG( (WORD)dwPos, 0 ) );
  316.                             SendMessage( hUpDown, UDM_SETRANGE, 0, dwRange );
  317.  
  318.                             EndDialog( hDlg, IDOK );
  319.                          }
  320.                          break;
  321.  
  322.                   case IDCANCEL :
  323.                          EndDialog( hDlg, IDCANCEL );
  324.                          break;
  325.                }
  326.                break;
  327.    }
  328.  
  329.    return (FALSE); 
  330. }
  331.  
  332.  
  333. LRESULT CALLBACK About( HWND hDlg,           
  334.                         UINT message,        
  335.                         WPARAM wParam,       
  336.                         LPARAM lParam)
  337. {
  338.    switch (message) 
  339.    {
  340.        case WM_INITDIALOG: 
  341.                return (TRUE);
  342.  
  343.        case WM_COMMAND:                              
  344.                if (   LOWORD(wParam) == IDOK         
  345.                    || LOWORD(wParam) == IDCANCEL)    
  346.                {
  347.                        EndDialog(hDlg, TRUE);        
  348.                        return (TRUE);
  349.                }
  350.                break;
  351.    }
  352.  
  353.    return (FALSE); 
  354. }
  355.